const {
	Controller
} = require("uni-cloud-router");
const uniID = require('uni-id');
module.exports = class UniIdController extends(
	Controller
) {
	async checkToken(event) {
		let data = event.data;
		let uniIdToken = data.uniIdToken;
		return await uniID.checkToken(uniIdToken);
	}

	async encryptPwd(event){
		let data = event.data;
		let password = data.password;
		return await uniID.encryptPwd(password);
	}

	async resetPwd(event){
		let data = event.data;
		let uid = data.uid;
		let password = data.password;
		return await uniID.resetPwd({
			uid: uid,
			password: password
		});
	}

	async setVerifyCode(event){
		let data = event.data;
		let mobile = data.mobile;
		let code = data.code;
		let type = data.type;

		return await uniID.setVerifyCode({
			mobile: mobile,
			code: code,
			expiresIn: 3000,
			type: type
		});
	}

	async verifyCode(event){
		let data = event.data;
		let mobile = data.mobile;
		let code = data.code;
		let type = data.type;
		return await uniID.verifyCode({
			mobile: mobile,
			code: code,
			type: type
		});
	}

	async bindMobile(event){
		let data = event.data;
		let uid = data.uid;
		let mobile = data.mobile;
		return await uniID.bindMobile({
			uid: uid,
			mobile: mobile
		});
	}

	async bindMobileBySms(event){
		let data = event.data;
		let uid = data.uid;
		let mobile = data.mobile;
		let code = data.code;
		return await uniID.bindMobile({
			uid: uid,
			mobile: mobile,
			code: code
		});
	}

	async register(event){
		let data = event.data;
		let username = data.username;
		let password = data.password;
		let nickname = data.nickname;
		return await uniID.register({
			username: username,
			password: password,
			nickname: nickname
		});
	}

	async login(event){
		let data = event.data;
		let username = data.username;
		let email = data.email;
		let mobile = data.mobile;
		let password = data.password;

		return await uniID.login({
			username: username,
			email: email,
			mobile: mobile,
			password: password
		});
	}

	async logout(event){
		let data = event.data;
		let uniIdToken = data.uniIdToken;
		return await uniID.logout(uniIdToken);
	}

	async loginBySms(event){
		let data = event.data;
		let mobile = data.mobile;
		let code = data.code;
		return await uniID.loginBySms({
			mobile,
			code
		});
	}

	async getUserInfo(event){
		let data = event.data;
		let uid = data.uid;
		return await uniID.getUserInfo({
			uid: uid
		});
	}

	async setUserInviteCode(event){
		let data = event.data;
		let uid = data.uid;
		let myInviteCode = data.myInviteCode;
		return await uniID.setUserInviteCode({
			uid: uid,
			myInviteCode: myInviteCode
		});
	}

	async getUserInfoByCode(event){
		let data = event.data;
		let uid = data.uid;
		let myInviteCode = data.myInviteCode;
		return await uniID.getUserInfo({
			uid: uid,
			my_invite_code: myInviteCode
		});
	}

	async getInvitedUser(event){
		let data = event.data;
		let uid = data.uid;
		let limit = data.limit;
		let offset = data.offset;
		let needTotal = data.needTotal;
		return await uniID.getInvitedUser({
			uid: uid,
			limit: limit,
			offset: offset,
			needTotal: needTotal
		});
	}

	async updatePwd(event){
		let data = event.data;
		let uid = data.uid;
		let oldPassword = data.oldPassword;
		let newPassword = data.newPassword;
		return await uniID.updatePwd({
			uid: uid,
			oldPassword: oldPassword,
			newPassword: newPassword
		});
	}

	async registerAdmin(event){
		let data = event.data;
		let username = data.username;
		let password = data.password;
		let role = data.role;
		return await uniID.register({
			username: username,
			password: password,
			role: role
		});
	}

	async registerUser(event){
		let data = event.data;
		return await uniID.register({
			...data
		});
	}

	async setAvatar(event){
		let data = event.data;
		let uid = data.uid;
		let avatar = data.avatar;
		return await uniID.setAvatar({
			uid: uid,
			avatar: avatar
		});
	}

	async updateUser(event){
		let data = event.data;
		return await uniID.updateUser({
			uid: data.uid,
			...data
		});
	}
};
